package work.mediway.mdm.biz.service.dict.impl;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import work.mediway.mdm.core.page.QueryRequest;
import work.mediway.mdm.biz.service.dict.CtBusinessFieldService;
import work.mediway.mdm.biz.vo.dict.BusinessFieldVO;
import work.mediway.mdm.core.enums.StatusEnum;
import work.mediway.mdm.biz.entity.dict.CtBusinessField;
import work.mediway.mdm.biz.mapper.dict.CtBusinessFieldMapper;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/1/7
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class CtBusinessFieldServiceImpl extends ServiceImpl<CtBusinessFieldMapper, CtBusinessField> implements CtBusinessFieldService {


    @Override
    public Page<BusinessFieldVO> pageBusiness(String param, QueryRequest queryRequest) {
        Page<BusinessFieldVO> page = new Page<>(queryRequest.getCurrent(), queryRequest.getSize());
        page.setRecords(this.baseMapper.listBusiness(null, param));
        for (BusinessFieldVO record : page.getRecords()) {
            record.setLastUpdateDateTime(StrUtil.join(" ", record.getLastUpdateDate(), record.getLastUpdateTime()));
            record.setStatusDesc(StatusEnum.map.get(record.getStatus()));
        }
        return page;
    }

    @Override
    public Page<BusinessFieldVO> pageBusinessHospital(QueryRequest queryRequest) {
        Page<BusinessFieldVO> page = new Page<>(queryRequest.getCurrent(), queryRequest.getSize());
        page.setRecords(this.baseMapper.listBusinessHospital(null));

        List<BusinessFieldVO> result = CollUtil.newArrayList();
        for (BusinessFieldVO record : page.getRecords()) {
            if (StrUtil.isAllNotBlank(record.getBusinessCode(), record.getHospitalCode())
                    && !StrUtil.equalsIgnoreCase(StringPool.NULL, record.getBusinessCode())
                    && !StrUtil.equalsIgnoreCase(StringPool.NULL, record.getHospitalCode())) {
                BusinessFieldVO vo = new BusinessFieldVO();
                vo.setBusinessHospitalCode(record.getBusinessCode() + StringPool.DASH + record.getHospitalCode());
                vo.setBusinessHospitalDesc(record.getBusinessDesc() + StringPool.DASH + record.getHospitalDesc());
                result.add(vo);
            }
        }
        page.setRecords(result);
        return page;
    }
}
